package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.entity.Event;
import com.antdesignbackend.backend.entity.Eventplann;
import com.antdesignbackend.backend.entity.Topic;
import com.antdesignbackend.backend.entity.entityVo.EventplannVo;
import com.antdesignbackend.backend.entity.entityVo.OrderVo;
import com.antdesignbackend.backend.entity.entityVo.TopicVo;
import com.antdesignbackend.backend.service.EventService;
import com.antdesignbackend.backend.service.EventplannService;
import com.antdesignbackend.backend.service.TopicService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/eventplann")
public class EventplannController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    ReturnVO result = new ReturnVO();
    @Autowired
    EventplannService eventplannService;
    @Autowired
    EventService eventService;
    private Integer id;
    /**
     *活动管理
     */
    @GetMapping("/index")
    @AuthToken
    public String index() {
        return "eventplann/index";
    }

    @GetMapping("/index2/{id}")
    @AuthToken
    public String index2(@PathVariable("id") int id) {
        this.id=id;
        return "event/index";
    }
    /**
     * 表格查询
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    public ReturnVO tableSearch(@RequestBody EventplannVo eventplannVo) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        System.out.println("进入活动策划！！！！！！！！！！！");
        List<EventplannVo> eventplannVos=new ArrayList<>();
        QueryWrapper<EventplannVo> eventplannVoQueryWrapper =new QueryWrapper<>();
        if(eventplannVo.getEventName()!=null&& !eventplannVo.getEventName().equals(""))
        {
            System.out.println("进入if语句");
            eventplannVoQueryWrapper.like("A.eventName",eventplannVo.getEventName());
        }
        //先统计满足条件的数据总数，因为sql 不包括排序
        int total=eventplannService.count(eventplannVoQueryWrapper);
        result.setTotal(total);
        System.out.println("*******************************满足条件的数据有："+total);
        //再查询
        if(total>=0)
        {
            eventplannVos=eventplannService.search(eventplannVoQueryWrapper,new Page<>(eventplannVo.getPageIndex(),eventplannVo.getPageSize()));
        }
        result.setData(eventplannVos);
        return result;
    }
    /**
     * 表格查询2
     */
    @PostMapping("/tableSearch2")
    @ResponseBody
    public ReturnVO tableSearch2() throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        System.out.println("进入明细查询！！！！！！！！！！！");
        QueryWrapper<Event> eventQueryWrapper=new QueryWrapper<>();
        eventQueryWrapper.eq("eventId",id);
        result.setData(eventService.page(new Page<>(1,10),eventQueryWrapper).getRecords());
        return result;
    }
    /**
     * 根据id删除
     */
    @DeleteMapping("/deleteById/{id}")
    @ResponseBody
    public ReturnVO deleteById(@PathVariable Serializable id) {
        result = new ReturnVO();
        QueryWrapper<Event> eventQueryWrapper=new QueryWrapper<>();
        eventQueryWrapper.eq("eventId",id);
        if (eventplannService.removeById(Integer.parseInt(id.toString())) == true && eventService.remove(eventQueryWrapper)) {
            result.setMessage("删除成功");
        } else {
            result.setMessage("删除失败");
            result.setCode("0");
        }
        return result;
    }
    /**
     * 修改/新增操作的保存
     */
    @PostMapping("/save")
    @ResponseBody
    @AuthToken
    public ReturnVO save(@RequestBody EventplannVo eventplannVo, HttpServletRequest request) throws Exception {
        System.out.println("活动vo的数据有："+eventplannVo);
        result = new ReturnVO();
        boolean b = false;
        if(eventplannVo.getId()==0){
            Eventplann eventplann=new Eventplann();
            Event event=new Event();
            eventplann.setEventName(eventplannVo.getEventName());
            eventplann.setEventRemark(eventplannVo.getEventRemark());
            eventplann.setEventStarTime(eventplannVo.getEventStarTime());
            eventplann.setEventEndTime(eventplannVo.getEventEndTime());
            b=eventplannService.save(eventplann);
            event.setEventAmount(eventplannVo.getEventAmount());
            event.setAmount(eventplannVo.getAmount());
            event.setIsUse(eventplannVo.getIsUse());
            event.setEventId(eventplann.getId());
            event.setIsReturn(eventplannVo.getIsReturn());
            if(eventplannVo.getYearReturn()==null){
                event.setYearReturn(0.0);
            }else{
                event.setYearReturn(eventplannVo.getYearReturn());
            }
            if(eventplannVo.getMonthReturn()==null){
                event.setMonthReturn(0.0);
            }else{
                event.setMonthReturn(eventplannVo.getMonthReturn());
            }
            event.setFrequency(eventplannVo.getFrequency());
            b=eventService.save(event);
        }else{
            System.out.println("修改的数据有"+eventplannVo);
            Eventplann eventplann=new Eventplann();
            Event event=new Event();
            UpdateWrapper<Eventplann> eventplannUpdateWrapper=new UpdateWrapper<>();
            eventplannUpdateWrapper.set("eventName",eventplannVo.getEventName()).set("eventRemark",eventplannVo.getEventRemark()).set("eventStarTime",eventplannVo.getEventStarTime()).set("eventEndTime",eventplannVo.getEventEndTime()).eq("id",eventplannVo.getId());
            b=eventplannService.update(eventplannUpdateWrapper);
            UpdateWrapper<Event> eventUpdateWrapper=new UpdateWrapper<>();
            eventUpdateWrapper.set("eventAmount",eventplannVo.getEventAmount()).set("amount",eventplannVo.getAmount()).set("isUse",eventplannVo.getIsUse()).set("isReturn",eventplannVo.getIsReturn()).set("yearReturn",eventplannVo.getYearReturn()).set("monthReturn",eventplannVo.getMonthReturn()).set("frequency",eventplannVo.getFrequency()).eq("eventId",eventplannVo.getId());
            b=eventService.update(eventUpdateWrapper);
        }
        if(b==true){
            result.setMessage("保存成功！");
            result.setCode("200");
        }
        else
        {
            result.setMessage("保存失败！");
            result.setCode("500");
        }
        return result;
    }
}
